package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.SysRoleEditResult;
import com.yh.homestay.dao.SysRoleMapper;
import com.yh.homestay.dao.SysRoleLikeMapper;
import com.yh.homestay.pojo.SysRole;
import com.yh.homestay.pojo.SysRoleExample;
import com.yh.homestay.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleLikeMapper sysoRoleMapper;
    @Override
    public HomeStayResult selectAll(String roleName, Integer page, Integer limit) {
        Map map=new HashMap();
        map.put("roleName",roleName);
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        PageHelper.startPage(page,limit);
        List<SysRole> sysRole = sysoRoleMapper.findByRoleName(map);
        PageInfo pageInfo = new PageInfo(sysRole);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public HomeStayResult update(Integer id, String roleName, String roleDesc) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(roleName);
        sysRole.setRoleDesc(roleDesc);
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(id);
        int update = sysRoleMapper.updateByExampleSelective(sysRole,example);
        if(update>0){
            return new HomeStayResult(200,"ok",0,null);
        }else {
            return new HomeStayResult(500,"error",0,null);
        }
    }

    @Override
    public HomeStayResult delete(Integer id) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(id);
        int delete = sysRoleMapper.delete(sysRole);
        if(delete>0){
            return new HomeStayResult(200,"ok",0,null);
        }else {
            return new HomeStayResult(500,"error",0,null);
        }
    }

    @Override
    public SysRoleEditResult selectOne(Integer id) {
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(id);
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
        if(sysRoles!=null&&sysRoles.size()>0){
            return new SysRoleEditResult(200,"ok",sysRoles.get(0));
        }else{
            return new SysRoleEditResult(500,"err",null);
        }
    }

    @Override
    public HomeStayResult delectAll(List<Integer> ids) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(ids);
        int delete = sysRoleMapper.deleteByExample(example);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public int addSysRole(SysRole sysRole) {
        int insert = sysRoleMapper.insert(sysRole);
        return insert;
    }
}
